﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataWorks_Tools.SpikeDetails
{
    public class BubbleSort
    {
        /// <summary>
        /// 冒泡升序
        /// </summary>
        /// <param name="rawData"></param>
        /// <returns></returns>
        public static double[] SortBubbleAscendingOrder(double[] rawData)
        {
            double[] outResult = new double[rawData.Length];
            unsafe
            {
                fixed (double* o = outResult, r = rawData)
                {
                    for (int i = 0; i < rawData.Length; i++)
                    {
                        o[i] = r[i];
                        for (int j = i; j > 0; j--)
                        {
                            if (o[j] < o[j - 1])
                            {
                                double t = o[j];
                                o[j] = o[j - 1];
                                o[j - 1] = t;
                            }
                        }
                    }
                }
            }
            return outResult;
        }
        /// <summary>
        /// 冒泡降序
        /// </summary>
        /// <param name="rawData"></param>
        /// <returns></returns>
        public static double[] SortBubbleDescendingOrder(double[] rawData)
        {
            double[] outResult = new double[rawData.Length];
            unsafe
            {
                fixed (double* o = outResult, r = rawData)
                {
                    for (int i = 0; i < rawData.Length; i++)
                    {
                        o[i] = r[i];
                        for (int j = i; j > 0; j--)
                        {
                            if (o[j] > o[j - 1])
                            {
                                double t = o[j];
                                o[j] = o[j - 1];
                                o[j - 1] = t;
                            }
                        }
                    }
                }
            }
            return outResult;
        }
    }
}
